

from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from goods.models import SKU,SKUSpecification,GoodsCategory,SPUSpecification
from meiduo_admin.serializers.sku_serializers import *
from meiduo_admin.custom_paginations import MyPage

class SKUViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer
    pagination_class = MyPage


    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(name__contains=keyword).order_by('id')
        return self.queryset.all().order_by('id')



class SKUCategoryListView(ListAPIView):
    queryset = GoodsCategory.objects.all()
    serializer_class = SKUCategorySimpleSerializer

    def get_queryset(self):
        return self.queryset.filter(parent_id__gte=38)


class SPUSimpleListView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleModelSerializer



class SKUSpecOptListView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SPUSpecModelSerializer

    def get_queryset(self):
        # 1、获得过滤的spu的主键
        # 思考：如何在一个非视图函数中，获得正则匹配路径参数？！
        # self.kwargs：该属性，封装了路由匹配之后，得到的所有命名分组参数
        spu_id = self.kwargs.get('pk')
        # 2、根据spu的主键，获取当前新增sku可选的规格模型类对象(数据集)
        spec_queryset = self.queryset.filter(spu_id=spu_id)
        # 3、返回规格数据集
        return spec_queryset












